package com.david.config;

import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

@Configuration
@org.mybatis.spring.annotation.MapperScan(basePackages = "com.david.dao")
@ConditionalOnBean({DataSource.class})
@EnableConfigurationProperties({MybatisProperties.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@AutoConfigureBefore(
        name = {"org.mybatis.mapper.autoconfigure.MapperAutoConfiguration"}
)
public class MybatisAutoConfiguration {
    public MybatisAutoConfiguration(MybatisProperties properties){

        if(!StringUtils.hasLength(properties.getTypeAliasesPackage())){
            properties.setTypeAliasesPackage("com.david.dto");
        }

        if (ObjectUtils.isEmpty(properties.resolveMapperLocations())) {
            properties.setMapperLocations(
                    new String[]{
                            "classpath*:com/david/mapper/*.xml",
                            "classpath*:com/david/dao/*.xml"});
        }
    }
}
